package com.test.down;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Handler;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.WindowManager;
import android.widget.Toast;

public class MyApplication extends Application {

    public static Context mContext;

    public static MyApplication instance;

    public static int width, height, screenDensity;

    private Handler mHandler=new android.os.Handler();

    @Override
    public void onCreate() {
        super.onCreate();
        mContext=getApplicationContext();
        instance=this;
        getScreenSize();
    }

    public static void getScreenSize()
    {
        WindowManager manage = (WindowManager) instance.getSystemService(WINDOW_SERVICE);
        Display display = manage.getDefaultDisplay();
        int screenHeight = display.getHeight();
        int screenWidth = display.getWidth();
        width = screenWidth;
        height = screenHeight;

        DisplayMetrics metrics = new DisplayMetrics();
        manage.getDefaultDisplay().getMetrics(metrics);
        screenDensity = metrics.densityDpi;
    }


    public static String obtainData(Context context, String name, String defaultValue) {
        if (context == null) return "";
        SharedPreferences preferences = context.getSharedPreferences("data", 0);
        return preferences.getString(name, defaultValue);
    }

    public static void saveData(Context context, String name, String value) {
        try {
            SharedPreferences preferences = context.getSharedPreferences("data", 0);
            SharedPreferences.Editor editor = preferences.edit();
            editor.putString(name, value);
            editor.commit();
        } catch (Exception e) {
        }
    }

    public void systemToast(final String message){
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(MyApplication.mContext,message,Toast.LENGTH_SHORT).show();
            }
        });
    }
}
